//***************************************************************************************
// d3dApp.cpp by Frank Luna (C) 2011 All Rights Reserved.
//***************************************************************************************

#include "d3dApp2.h"

using namespace DirectX;

D3DApp2::D3DApp2(HINSTANCE hInstance)
: D3DApp(hInstance)
{
}

D3DApp2::~D3DApp2()
{
}

LRESULT D3DApp2::MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    if (msg == WM_KEYDOWN)
    {
        if (wParam == VK_ESCAPE)
        {
            SendMessage(hwnd, WM_CLOSE, 0, 0);
            return 0;
        }
    }

    return D3DApp::MsgProc(hwnd, msg, wParam, lParam);
}

void D3DApp2::OnMouseDown(WPARAM btnState, int x, int y)
{
    mLastMousePos.x = x;
    mLastMousePos.y = y;

    SetCapture(MainWnd());
}

void D3DApp2::OnMouseUp(WPARAM btnState, int x, int y)
{
    ReleaseCapture();
}

void D3DApp2::OnMouseMove(WPARAM btnState, int x, int y)
{
    if ((btnState & MK_LBUTTON) != 0)
    {
        // Make each pixel correspond to a quarter of a degree.
        float dx = XMConvertToRadians(0.25f*static_cast<float>(x - mLastMousePos.x));
        float dy = XMConvertToRadians(0.25f*static_cast<float>(y - mLastMousePos.y));

        // Update angles based on input to orbit camera around box.
        mTheta += dx;
        mPhi += dy;

        // Restrict the angle mPhi.
        mPhi = MathHelper::Clamp(mPhi, 0.1f, MathHelper::Pi - 0.1f);

#if defined(DEBUG) || defined(_DEBUG)
        char buf[1024];
        sprintf_s(buf, "mTheta = %f, mPhi = %f\n", XMConvertToDegrees(mTheta), XMConvertToDegrees(mPhi));
        OutputDebugStringA(buf);
#endif
    }
    else if ((btnState & MK_RBUTTON) != 0)
    {
        // Make each pixel correspond to 0.005 unit in the scene.
        float dx = 0.005f*static_cast<float>(x - mLastMousePos.x);
        float dy = 0.005f*static_cast<float>(y - mLastMousePos.y);

        // Update the camera radius based on input.
        mRadius += dx - dy;

        // Restrict the radius.
        mRadius = MathHelper::Clamp(mRadius, 3.0f, 15.0f);

#if defined(DEBUG) || defined(_DEBUG)
        char buf[1024];
        sprintf_s(buf, "mRadius = %f\n", mRadius);
        OutputDebugStringA(buf);
#endif
    }

    mLastMousePos.x = x;
    mLastMousePos.y = y;
}
